/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RefTweetView extends LinearLayout {
	TextView HeadText,ContentText;
	ImageButton CloseImBtn;
	boolean isdm=false;
	
	public final static int MODE_RPY_MSG 	= 0;
	public final static int MODE_RPY_USR	= 1;
	@Override
	public String toString() {
		if (this.getVisibility() == VISIBLE) {
			return HeadText.getText().toString();
		} else {
			return "";
		}
	}

	public final static int MODE_RT	= 2;
	public final static int MODE_DM_MSG	= 3;
	public final static int MODE_DM_USR	= 4;
	
	public RefTweetView(Context context) {
		super(context);
		initView();
	}

	public RefTweetView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	private void initView() {
		LayoutInflater.from(getContext()).inflate(R.layout.ref_tweet, this,true);
		HeadText = (TextView)this.findViewById(R.id.head);
		ContentText = (TextView)this.findViewById(R.id.content);
		CloseImBtn = (ImageButton)this.findViewById(R.id.ref_close);
		CloseImBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isdm) {
					new AlertDialog.Builder(getContext())
						.setTitle("操作确认")
						.setMessage("退出私信模式?")
						.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									RefTweetView.this.setVisibility(GONE);
								}
							}
						)
						.setNegativeButton("取消", null)
						.show();
				} else {
					RefTweetView.this.setVisibility(GONE);
				}
			}
		});
	}
	
	public void update(TweetObject data,int mode) {
		switch (mode) {
		case MODE_RPY_MSG:
			HeadText.setText(String.format("-@ %d", data.id));
			isdm = false;
			break;
		case MODE_RT:
			HeadText.setText(String.format("-rt %d", data.id));
			isdm = false;
			break;
		case MODE_DM_MSG:
			HeadText.setText(String.format("-d %s", data.user));
			isdm = true;
			break;
		default:
			Log.w("Getwit","RefTweetView: unknown mode");
		}
		ContentText.setText(new StringBuilder(data.user).append(": ").append(data.content));
		ContentText.setVisibility(VISIBLE);
		setVisibility(VISIBLE);
	}
	
	public void update(String user,int mode)
	{
		switch (mode) {
		case MODE_RPY_USR:
			HeadText.setText(String.format("-@ %s", user));
			isdm = false;
			break;
		case MODE_DM_USR:
			HeadText.setText(String.format("-d %s", user));
			isdm = true;
			break;
		default:
			Log.w("Getwit","RefTweetView: unknown mode");
		}
		ContentText.setVisibility(GONE);
		setVisibility(VISIBLE);
	}
	
	public void hide()
	{
		setVisibility(GONE);
	}

}
